var PlayerHealthController = require("PlayerHealthController");
var PlayerController = require("PlayerController");
var ExperienceLevelController = require("ExperienceLevelController");
var EnemySpawner = require("EnemySpawner");
var CoinController = require("CoinController");
var SFXManager = require("SFXManager");
var DamageNumberManager = require("DamageNumberManager");


var EnemyController = cc.Class({
    extends: cc.Component,

    properties: {
        rigidBody: cc.RigidBody,

        moveSpeed: 0,
        // 造成的伤害
        damage: 0,

        // 受击间隔
        hitWaitTime: 0.5,
        hitCounter: 0,

        // 击退间隔
        repelTime: 0.15,
        repelCounter: 0,
        // 血量
        health: 10,
        // 掉落的钱
        coinValue: cc.Integer,
        coinDropRate: 0.5,
    },

    // LIFE-CYCLE CALLBACKS:

    start () {
        // 获取Player位置
        this.playerNodePosition = PlayerController.instance.playerNode.getComponent(cc.RigidBody).getWorldPosition();
    },

    update (dt) {
        if (PlayerController.instance.playerNode.active == true) {
            this.playerNodePosition = PlayerController.instance.playerNode.getComponent(cc.RigidBody).getWorldPosition();

            if (this.repelCounter > 0) {
                this.repelCounter -= dt;

                if (this.moveSpeed > 0) {
                    this.moveSpeed = -this.moveSpeed * 3;
                }

                if (this.repelCounter <= 0) {
                    this.moveSpeed = Math.abs(this.moveSpeed / 3);
                }
            }

            // 设置刚体速度（这里注意要计算的是在世界坐标下的速度向量）
            this.rigidBody.linearVelocity = this.playerNodePosition.sub(this.rigidBody.getWorldPosition()).normalize().mulSelf(this.moveSpeed);
            //this.rigidBody.angleVelocity = cc.v2(0,1).signAngle(this.playerNodePosition.sub(this.node.position).normalizeSelf()) / 180 * Math.PI;

            if (this.hitCounter > 0) {
                this.hitCounter -= dt;
            }
        } else {
            this.rigidBody.linearVelocity = cc.v2(0, 0);
        }
    },

    // 敌人动画镜像翻转
    reverseScaleX (){
        let sprite = this.node.getChildByName('Sprite');
        if (this.rigidBody.linearVelocity.x * sprite.scaleX < 0) {
            sprite.scaleX = -sprite.scaleX;
        }
    },

    onBeginContact(contact, selfCollider, otherCollider) {
        // if (otherCollider.node.group == 'Player' && this.hitCounter <= 0) {
        //     PlayerHealthController.instance.TakeDamage(this.damage);

        //     this.hitCounter = this.hitWaitTime;
        // }
    },

    onCollisionEnter (other, self) {
        if (other.node.group == 'Player' && this.hitCounter <= 0) {
            PlayerHealthController.instance.TakeDamage(this.damage);

            this.hitCounter = this.hitWaitTime;
        }
    },
    

    TakeDamage (damage) {
        this.health -= damage;
        DamageNumberManager.instance.spawnDamageNumber(this.node.position, damage);

        if (this.health <= 0) {
            let exp = ExperienceLevelController.instance.spawnExp(this.node.position);
            this.node.parent.addChild(exp);
            if (Math.random() > this.coinDropRate) {
                let coin = CoinController.instance.dropCoins(this.node.position, this.coinValue);
                this.node.parent.addChild(coin);
            }
            this.node.destroy();
            SFXManager.instance.playSFX(8);
            //EnemySpawner.instance.spawnedEnemiesPool.put(this.node);
        } else {
            SFXManager.instance.playSFX(3);
        }
    },

    TakeDamageForRepel (damage) {
        this.TakeDamage(damage);

        this.repelCounter = this.repelTime;
    },

    // 后面再用对象池
    // unuse () {
    //     console.log("spawn",EnemySpawner.instance.spawnedEnemiesPool.size());
    // },

    // reuse () {
    //     console.log("recycle",EnemySpawner.instance.spawnedEnemiesPool.size());
    // },
});

module.exports = EnemyController;
